<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="baseSalary">
      <el-input v-model="dataForm.baseSalary" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="practiceSalary">
      <el-input v-model="dataForm.practiceSalary" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="workDay">
      <el-input v-model="dataForm.workDay" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="postUp">
      <el-input v-model="dataForm.postUp" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="houseSup">
      <el-input v-model="dataForm.houseSup" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ded">
      <el-input v-model="dataForm.ded" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="overtimePay">
      <el-input v-model="dataForm.overtimePay" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lateNum">
      <el-input v-model="dataForm.lateNum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          baseSalary: '',
          practiceSalary: '',
          workDay: '',
          postUp: '',
          houseSup: '',
          ded: '',
          overtimePay: '',
          lateNum: '',
          isDeleted: ''
        },
        dataRule: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          baseSalary: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          practiceSalary: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          workDay: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          postUp: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          houseSup: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ded: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          overtimePay: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lateNum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/work/pcsalary/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.pcSalary.name
                this.dataForm.baseSalary = data.pcSalary.baseSalary
                this.dataForm.practiceSalary = data.pcSalary.practiceSalary
                this.dataForm.workDay = data.pcSalary.workDay
                this.dataForm.postUp = data.pcSalary.postUp
                this.dataForm.houseSup = data.pcSalary.houseSup
                this.dataForm.ded = data.pcSalary.ded
                this.dataForm.overtimePay = data.pcSalary.overtimePay
                this.dataForm.lateNum = data.pcSalary.lateNum
                this.dataForm.isDeleted = data.pcSalary.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/work/pcsalary/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'baseSalary': this.dataForm.baseSalary,
                'practiceSalary': this.dataForm.practiceSalary,
                'workDay': this.dataForm.workDay,
                'postUp': this.dataForm.postUp,
                'houseSup': this.dataForm.houseSup,
                'ded': this.dataForm.ded,
                'overtimePay': this.dataForm.overtimePay,
                'lateNum': this.dataForm.lateNum,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
